package com.shop.trade.api.service.impl;

import com.shop.common.model.InputBean;
import com.shop.common.model.ServiceBean;
import com.shop.common.model.param.trade.ShoppingCartInput;
import com.shop.common.model.result.item.ItemBaseOutput;
import com.shop.common.model.result.item.ItemDetailsOutput;
import com.shop.common.model.result.trade.ShoppingCartOutput;
import com.shop.common.model.result.user.UserBaseOutput;
import com.shop.common.tmpl.BaseService;
import com.shop.common.util.DateUtil;
import com.shop.common.util.state.DataStatus;
import com.shop.trade.api.service.IShopCartSvc;
import com.shop.trade.atom.atomsvc.IShopCartAtomSvc;
import com.shop.trade.feign.item.IItemAtomFeignClientSvc;
import com.shop.trade.feign.user.IUserAtomFeignClientSvc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 *
 * @author zhoulk
 * @date: 2019年04月05日  15:08
 */
@Service
public class ShopCartSvc extends BaseService implements IShopCartSvc {

    @Autowired
    IShopCartAtomSvc iShopCartAtomSvc;
    @Autowired
    IItemAtomFeignClientSvc iItemAtomFeignClientSvc;
    @Autowired
    IUserAtomFeignClientSvc iUserAtomFeignClientSvc;

    @Override
    public ServiceBean<Integer> addToShopCart(ShoppingCartInput shoppingCartInput) {
        ItemBaseOutput itemBaseOutput = iItemAtomFeignClientSvc.qryItemBase(shoppingCartInput.getItemId());
        if (itemBaseOutput == null) {
            return resultFailed("该商品不存在");
        }
        if (itemBaseOutput.getUserId().equals(shoppingCartInput.getUserId())) {
            return resultFailed("不能加购自己发布的商品");
        }

        ItemDetailsOutput itemDetailsOutput = iItemAtomFeignClientSvc.qryItemDetails(shoppingCartInput.getItemId());
        ShoppingCartInput input = new ShoppingCartInput();
        input.setItemId(shoppingCartInput.getItemId());
        input.setUserId(shoppingCartInput.getUserId());
        ServiceBean<ShoppingCartOutput> serviceBean = iShopCartAtomSvc.selectShoppingCart(shoppingCartInput);

        if (serviceBean.isSuccess()) {
            ShoppingCartOutput shoppingCartOutput = serviceBean.getData();
            shoppingCartInput = new ShoppingCartInput();
            shoppingCartInput.setId(shoppingCartOutput.getId());
            int itemNum = (shoppingCartOutput.getItemNum() + 1) > itemDetailsOutput.getStock() ? itemDetailsOutput.getStock() : (shoppingCartOutput.getItemNum() + 1);
            shoppingCartInput.setItemNum(itemNum);
            shoppingCartInput.setModifyTime(DateUtil.newNowDate());
            return iShopCartAtomSvc.updateShoppingCart(shoppingCartInput);
        } else {
            shoppingCartInput.setCreateTime(DateUtil.newNowDate());
            shoppingCartInput.setItemNum(1);
            shoppingCartInput.setMchtId(itemBaseOutput.getUserId());
            shoppingCartInput.setDataStatus(DataStatus.USE.value());
            return iShopCartAtomSvc.insertShoppingCart(shoppingCartInput);
        }
    }

    @Override
    public ServiceBean<List<ShoppingCartOutput>> qryShopCarts(ShoppingCartInput shoppingCartInput) {
        return iShopCartAtomSvc.selectShoppingCartList(shoppingCartInput);
    }

    @Override
    public ServiceBean<Map<Integer, List<ShoppingCartOutput>>> qryShoppingCarts4P(InputBean<ShoppingCartInput> inputBean) {
        ServiceBean<Map<Integer, List<ShoppingCartOutput>>> result = new ServiceBean<>();
        ServiceBean<List<ShoppingCartOutput>> serviceBean = iShopCartAtomSvc.qryShoppingCarts4P(inputBean);
        if (serviceBean.isSuccess()) {
            List<ShoppingCartOutput> list = serviceBean.getData();
            Map<Integer, List<ShoppingCartOutput>> map = new LinkedHashMap<>();
            for (ShoppingCartOutput shopCart : list) {
                ItemBaseOutput itemBaseOutput = iItemAtomFeignClientSvc.qryItemBase(shopCart.getItemId());
                ItemDetailsOutput itemDetailsOutput = iItemAtomFeignClientSvc.qryItemDetails(shopCart.getItemId());
                shopCart.setDiscountPrice(itemBaseOutput.getDiscountPrice());
                shopCart.setSellingPrice(itemBaseOutput.getSellingPrice());
                shopCart.setMainImg(itemDetailsOutput.getMainImg());
                shopCart.setItemName(itemBaseOutput.getItemName());
                shopCart.setStatus(itemBaseOutput.getStatus());
                if (map.containsKey(shopCart.getMchtId())) {
                    map.get(shopCart.getMchtId()).add(shopCart);
                } else {
                    List<ShoppingCartOutput> shopCartList = new ArrayList<>();
                    shopCartList.add(shopCart);
                    UserBaseOutput userBaseOutput = iUserAtomFeignClientSvc.qryUserBaseByUserId(shopCart.getMchtId());
                    shopCart.setMchtName(userBaseOutput.getNickname());
                    map.put(shopCart.getMchtId(), shopCartList);
                }
            }
            result.setSuccess(true);
            result.setData(map);
        } else {
            result.setSuccess(false);
            result.setMsg(serviceBean.getMsg());
        }
        return result;
    }

    @Override
    public ServiceBean<String> delShopCart(List<Integer> ids) {
        return iShopCartAtomSvc.deleteShoppingCart(ids);
    }

    @Override
    public ServiceBean<Integer> modShopCartItemNum(ShoppingCartInput shoppingCartInput) {
        ItemDetailsOutput itemBaseOutput = iItemAtomFeignClientSvc.qryItemDetails(shoppingCartInput.getItemId());
        ServiceBean<Integer> serviceBean = new ServiceBean<>();
        if (shoppingCartInput.getItemNum() > itemBaseOutput.getStock()) {
            shoppingCartInput.setItemNum(itemBaseOutput.getStock());
            serviceBean.setMsg("商品库存不足，购物车数量已更改为商品最大库存");
        }
        serviceBean.setData(shoppingCartInput.getItemNum());
        serviceBean.setSuccess(iShopCartAtomSvc.updateShoppingCart(shoppingCartInput).isSuccess());
        return serviceBean;
    }
}
